package com.flexcloudsdk.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CloudFilesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(CloudFilesServlet.class
			.getName());
	private String sourceMethod;
	private String sourceClass;

	public CloudFilesServlet() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudFilesServlet.class.getName();
		log.info("*****CloudFilesServlet constructor********");
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudFilesServlet.class.getName();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		resp.setContentType("text/xml");
		PrintWriter writer = resp.getWriter();
		writer.println("<?xml version=\"1.0\"?>");
		writer.println("<root>");

		String operation = req.getParameter("operation");
		if (operation == null) {
			handleNullOperation(writer, req);
		} else if (operation.isEmpty()) {
			handleEmptyOperation(writer, req);
		}	else {
			CloudMethods enumval = CloudMethods.valueOf(operation);
			switch (enumval) {
			case uploadBinaryFileMethod:
				uploadBinaryFile(writer, req, resp);
				break;
			case uploadTextFileMethod:
				uploadTextFile(writer, req, resp);
				break;
			case downloadBinaryFileMethod:
				downloadBinaryFile(writer, req, resp);
				break;
			case downloadTextFileMethod	:
				downloadTextFile(writer, req, resp);
				break;
			default:
				log.severe("Uknown method did the sdk get updated?");
				break;
		    }
		}	
		writer.println("</root>");
	}

	private void downloadTextFile(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
	}

	private void downloadBinaryFile(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
	}

	private void uploadTextFile(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
	}

	private void uploadBinaryFile(PrintWriter writer, HttpServletRequest req,
			HttpServletResponse resp) {
	}

	private void handleNullOperation(PrintWriter writer, HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		log.warning("operation is null");
		writer.println("<result>No operation found while calling "
				+ req.getServerName() + "servlet</result>");
	}

	private void handleEmptyOperation(PrintWriter writer, HttpServletRequest req) {
		log.warning("operation is empty");
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		writer.println("<result>Must pass an operation in calling "
				+ req.getServerName() + "servlet</result>");
	}
	
	enum CloudMethods {
		uploadBinaryFileMethod, uploadTextFileMethod, downloadBinaryFileMethod, downloadTextFileMethod
	};

	public static final String uploadBinaryFileCall = "uploadBinaryFile";
	public static final String uploadTextFileCall = "uploadTextFile";
	public static final String downloadBinaryFileCall = "downloadBinaryFile";
	public static final String downloadTextFileCall  = "downloadTextFile";

}
